<!-- 经销商后结算模块-财务管理-采购管理-采购单详情 -->
<template>
  <div class="auth-product hospital-settlement-box">
    <el-container class="partDetails is-vertical">
      <tabs
        :tabs-arr="tabSliceList"
        @returnIndex="tabsHandle" />
      <record-info
        class="custom-info"
        :record-form="purcharseCommonForm"
        :record-data="headerInfo">
        <template slot="recordInfoslot">
          <div
            class="solt-info"
            v-if="activeIndex === 0">
            <span class="left-span">
              采购产品总数
              <span class="right-span">{{ headerInfo.prodTotalCount }}</span>
            </span>
            <span>
              品种数
              <span>{{ headerInfo.prodTypeCount }}</span>
            </span>
            <span>
              金额合计 （元）:
              <span>
                <span class="price">{{ headerInfo.orderAmount | getFormatterPrice }}</span>
              </span>
            </span>
          </div>
          <div
            class="solt-info"
            v-if="activeIndex === 1">
            <span class="left-span">
              收货单号
              <span class="right-span">{{ headerInfo.receiveCode }}</span>
            </span>
            <span>
              收货时间
              <span class="right-span">{{ headerInfo.receiveTime }}</span>
            </span>
            <span>
              收货人
              <span class="right-span">{{ headerInfo.receiver }}</span>
            </span>
          </div>
          <div
            class="solt-info"
            v-if="activeIndex === 1">
            <span class="left-span">
              收货产品总数
              <span>{{ headerInfo.receiveCount }}</span>
            </span>
            <span>
              品种数
              <span>{{ headerInfo.receiveTypeCount }}</span>
            </span>
            <span>
              金额合计 （元）
              <span class="price">{{ headerInfo.receiveTotalAmount | getFormatterPrice }}</span>
            </span>
          </div>
        </template>
      </record-info>
      <el-editable
        class="hospital-edit-box"
        :data="activeList"
        :key="activeIndex"
        height="400"
        v-loading="tableLoading"
        :columns="activeColumns"
      >
        <template
          slot="default-pssRegistrationNumber"
          slot-scope="scope">
          <el-button
            :disabled="!scope.row.pssRegistrationNumber||!scope.row.pssRegistrationId"
            v-html="regisNoButton(scope.row, 'pssRegistration')"
            @click="showPdfRegistion(scope.row.pssRegistrationId)"
            type="text"
          />
        </template>
        <template
          slot="default-hspRegistrationNumber"
          slot-scope="scope">
          <el-button
            :disabled="!scope.row.hspRegistrationNumber||!scope.row.hspRegistrationId"
            v-html="regisNoButton(scope.row, 'hspRegistration')"
            @click="showPdfRegistion(scope.row.hspRegistrationId)"
            type="text"
          />
        </template>
      </el-editable>
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="curPage"
        :page-sizes="PAGE_NUMBER"
        :page-size="pageSize"
        :layout="PAGINATION_LAYOUT"
        :total="totalElements"
      />
    </el-container>
  </div>
</template>

<script>
import {
  distributorPurchaseDetail,
  distributorPurchaseReceiveDetail
} from '@const/beforeSettleApi'
import {
  partOrderColumns,
  partReceiveColumns,
  detailTabsArr,
  purcharseCommonForm,
  statusArr
} from './const'
import tabs from '@components/tabs/index.vue'
import { PAGE_NUMBER, PAGINATION_LAYOUT } from '@const/common'
import RecordInfo from '@/moduleComponents/beforeSettle/distributorSide/purchasingManagement/record'
import { attrItemList } from '@const/orderCenterApi'
import ElEditable from '@components/el-editable'
import { cloneDeep, isObject, hasIn } from 'lodash'
import { getFormatterPrice } from '@const/filter'
import { getCertificate } from '@const/productCenterApiV2'
export default {
  components: { ElEditable, tabs, RecordInfo },
  data () {
    return {
      id: this.$route.query.id,
      orderStatus: null,
      // 表格Loading
      tableLoading: false,
      // 表格数据
      orderLists: [],
      // 表单 详情展示内容-table
      columnsLists: [partOrderColumns, partReceiveColumns],
      purcharseCommonForm,
      // 详情tab
      detailTabsArr,
      curPage: 1, // 当前页
      PAGE_NUMBER, // 页码数组
      PAGINATION_LAYOUT, // 页码功能布局
      pageSize: PAGE_NUMBER[0], // 一页的数量
      totalElements: 0, // 总共的条数
      activeIndex: 0,
      // 订单详情信息
      headerInfo: {}
    }
  },
  watch: {
    activeIndex (val) {
      this.curPage = 1 // 当前页
      this.pageSize = PAGE_NUMBER[0] // 一页的数量
      this.totalElements = 0 // 总共的条数
    }
  },
  mounted () {
    this.getList()
  },
  filters: {
    getFormatterPrice
  },
  computed: {
    tabSliceList () {
      // 订单状态（0-全部，21-待提交；22-待供应商响应；23-待供应商发货；24-待收货；25-收货完成；26-已撤消；27-已拒绝）
      let detailTabsArr = cloneDeep(this.detailTabsArr)
      // 14-收货完成--可以查看收货信息
      if (![25].includes(this.orderStatus)) {
        detailTabsArr[1].disabled = true
      }
      return detailTabsArr
    },
    activeList () {
      return this.orderLists[this.activeIndex]
    },
    activeColumns () {
      return this.columnsLists[this.activeIndex]
    }
  },
  methods: {
    /**
     * @param [Number] index
     * @desc 通过index选择请求方法 -- 获取--0:订单信息/1:收货信息
     */
    async getList (index = this.activeIndex) {
      this.tableLoading = true
      // 表格字段名称 订单详情--getOrderList 发货详情--getReceiveList
      const requestList = [this.getOrderList, this.getReceiveList]
      const request = requestList[index]
      const { infoList, ...baseInfo } = await request()
      Object.keys(baseInfo).forEach(key => {
        if (key === 'orderStatus') {
          this.orderStatus = baseInfo[key]
          baseInfo[key] = statusArr[baseInfo[key]]
        }
        this.$set(this.headerInfo, key, baseInfo[key] || '--')
      })
      this.$set(this.orderLists, index, infoList.content)
      this.totalElements = infoList.totalElements || 0
      this.tableLoading = false
    },
    showPdfRegistion (id) {
      this.$axios({
        method: 'get',
        url: getCertificate,
        params: {
          id: id
        }
      }).then(res => {
        let obj = hasIn(res, 'data.payload') ? res.data.payload : {}
        let url = obj.pictureUrl
        if (!url) {
          this.$message({
            type: 'warning',
            message: '获取失败，请在产品管理中维护该产品的注册证信息'
          })
        } else {
          this.$pdfViewer({ url, title: '注册证pdf预览' })
        }
      })
    },
    // 详情-采购单详情
    async getOrderList () {
      // 表格字段名称 订单详情--purchaseProductList 收货详情--receiveProductList
      const url = distributorPurchaseDetail
      const data = {
        page: this.curPage,
        pageSize: this.pageSize,
        purchaseOrderId: this.id
      }
      const res = await this.$axios.$post(url, data)
      const { purchaseProductList, ...baseInfo } = res.payload
      return { ...baseInfo, infoList: purchaseProductList }
    },
    // 详情-收货详情
    async getReceiveList () {
      const url = distributorPurchaseReceiveDetail
      const data = {
        page: this.curPage,
        pageSize: this.pageSize,
        purchaseOrderId: this.id
      }
      const res = await this.$axios.$post(url, data)
      const { receiveProductList, ...baseInfo } = res.payload
      return { ...baseInfo, infoList: receiveProductList }
    },
    // tab切换
    tabsHandle (index) {
      this.activeIndex = index
      this.curPage = 1 // 当前页
      this.pageSize = PAGE_NUMBER[0] // 一页的数量
      this.getList()
    },
    regisNoButton (data, str = 'registration') {
      const number = `${str}Number`
      const id = `${str}Id`
      if (data[number]) {
        return data[number]
      } else if (!data[number] && data[id]) {
        return '暂无注册证号'
      } else if (!data[number] && !data[id]) {
        return '暂无数据'
      }
    },
    // 页码更改
    handleSizeChange (size) {
      this.pageSize = size
      this.getList()
    },

    // 切换不一样的页面,重置一切数据
    handleCurrentChange (curPage) {
      this.curPage = curPage
      this.getList()
    }
  }
}
</script>

<style lang="less">
.hospital-settlement-box {
  .partDetails {
    height: 100%;
    .custom-info {
      margin: 0 0 20px 0;
    }
    .pagination {
      padding-top: 10px;
      display: flex;
      justify-content: flex-end;
      padding-right: 0;
    }
  }
}
</style>
